{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import keras\n",
    "import tensorflow as tf\n",
    "\n",
    "from keras.datasets import mnist\n",
    "from keras.models import Sequential\n",
    "from keras.layers import Dense, Dropout\n",
    "from keras.optimizers import RMSprop\n",
    "\n",
    "from sklearn.preprocessing import StandardScaler, MinMaxScaler, Normalizer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "batch_size = 128\n",
    "num_classes = 10\n",
    "epochs = 20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n",
    "\n",
    "x_train = x_train.reshape(60000, 784).astype('float32')\n",
    "x_test = x_test.reshape(10000, 784).astype('float32')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "sc = MinMaxScaler(feature_range=(0,1))\n",
    "\n",
    "x_train = sc.fit_transform(x_train)\n",
    "x_test = sc.fit_transform(x_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# one hot encoding\n",
    "y_train = keras.utils.to_categorical(y_train, num_classes)\n",
    "y_test = keras.utils.to_categorical(y_test, num_classes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "model = Sequential()\n",
    "model.add(Dense(800, activation='relu', input_shape=(784,)))\n",
    "model.add(Dense(10, activation='softmax'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model.compile(loss='categorical_crossentropy',\n",
    "              optimizer=RMSprop(),\n",
    "              metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 60000 samples, validate on 10000 samples\n",
      "Epoch 1/20\n",
      "60000/60000 [==============================] - 2s - loss: 0.2379 - acc: 0.9308 - val_loss: 0.1137 - val_acc: 0.9660\n",
      "Epoch 2/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0921 - acc: 0.9721 - val_loss: 0.0882 - val_acc: 0.9729\n",
      "Epoch 3/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0602 - acc: 0.9819 - val_loss: 0.0772 - val_acc: 0.9759\n",
      "Epoch 4/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0421 - acc: 0.9872 - val_loss: 0.0717 - val_acc: 0.9778\n",
      "Epoch 5/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0311 - acc: 0.9905 - val_loss: 0.0733 - val_acc: 0.9782\n",
      "Epoch 6/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0234 - acc: 0.9928 - val_loss: 0.0676 - val_acc: 0.9813\n",
      "Epoch 7/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0175 - acc: 0.9946 - val_loss: 0.0670 - val_acc: 0.9819\n",
      "Epoch 8/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0129 - acc: 0.9961 - val_loss: 0.0721 - val_acc: 0.9816\n",
      "Epoch 9/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0100 - acc: 0.9968 - val_loss: 0.0670 - val_acc: 0.9834\n",
      "Epoch 10/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0081 - acc: 0.9976 - val_loss: 0.0726 - val_acc: 0.9829\n",
      "Epoch 11/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0063 - acc: 0.9981 - val_loss: 0.0739 - val_acc: 0.9828\n",
      "Epoch 12/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0046 - acc: 0.9986 - val_loss: 0.0852 - val_acc: 0.9810\n",
      "Epoch 13/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0044 - acc: 0.9987 - val_loss: 0.0806 - val_acc: 0.9831\n",
      "Epoch 14/20\n",
      "60000/60000 [==============================] - 2s - loss: 0.0030 - acc: 0.9992 - val_loss: 0.0770 - val_acc: 0.9844\n",
      "Epoch 15/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0024 - acc: 0.9994 - val_loss: 0.0857 - val_acc: 0.9834\n",
      "Epoch 16/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0020 - acc: 0.9993 - val_loss: 0.0856 - val_acc: 0.9842\n",
      "Epoch 17/20\n",
      "60000/60000 [==============================] - 2s - loss: 0.0014 - acc: 0.9996 - val_loss: 0.1032 - val_acc: 0.9819\n",
      "Epoch 18/20\n",
      "60000/60000 [==============================] - 2s - loss: 0.0015 - acc: 0.9996 - val_loss: 0.1036 - val_acc: 0.9816\n",
      "Epoch 19/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0013 - acc: 0.9997 - val_loss: 0.1034 - val_acc: 0.9816\n",
      "Epoch 20/20\n",
      "60000/60000 [==============================] - 1s - loss: 0.0012 - acc: 0.9997 - val_loss: 0.1036 - val_acc: 0.9819\n"
     ]
    }
   ],
   "source": [
    "history = model.fit(x_train, y_train,\n",
    "                    batch_size=batch_size,\n",
    "                    epochs=epochs,\n",
    "                    verbose=1,\n",
    "                    validation_data=(x_test, y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Global TF Kernel (Python 3)",
   "language": "python",
   "name": "global-tf-python-3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
